const koa = require('koa')
// 路由相关
const controller = require('koa-route')
// 模板渲染相关
const views = require('co-views')
// 静态资源访问
const koa_static = require('koa-static-server')
const service = require('./service/webAppService')
// 请求query
let qs = require('querystring')
// 代理请求
// const proxy = require("http-proxy-middleware");
const proxy = require('koa-proxy')

let app = koa()
let render = views('./view', {
  map: {
    html: 'ejs'
  }
})

app.use(
  koa_static({
    rootDir: './static/', //文件系统资源路径
    rootPath: '/static/', //url地址资源路径
    maxage: 0 //缓存 不
  })
)

app.use(
  controller.get('/route_test', function*() {
    this.set('Cache-Control', 'no-cache')
    this.body = 'hello koa!'
  })
)

app.use(
  controller.get('/test', function*() {
    this.set('Cache-Control', 'no-cache')
    this.body = yield render('test')
  })
)

app.use(
  controller.get('/ejs_test', function*() {
    this.set('Cache-Control', 'no-cache')
    this.body = yield render('test', {
      title: 'test_title'
    })
  })
)

app.use(
  controller.get('/', function*() {
    this.set('Cache-Control', 'no-cache')
    this.body = yield render('index', { title: '书城首页' })
  })
)

app.use(
  controller.get('/search', function*() {
    this.set('Cache-Control', 'no-cache')
    this.body = yield render('search')
  })
)

app.use(
  controller.get('/book', function*() {
    this.set('Cache-Control', 'no-cache')
    let params = qs.parse(this.req._parsedUrl.query)
    let bookId = params.id || ''
    this.body = yield render('book', { bookId })
  })
)

app.use(
  controller.get('/reader', function*() {
    this.set('Cache-Control', 'no-cache')
    this.body = yield render('reader')
  })
)

app.use(
  controller.get('/api_test', function*() {
    this.set('Cache-Control', 'no-cache')
    this.body = service.get_test_data()
  })
)

app.use(
  controller.get('/ajax/search', function*() {
    this.set('Cache-Control', 'nocache')
    let params = qs.parse(this.req._parsedUrl.query)
    let start = params.start
    let end = params.end
    let keyword = params.keyword
    console.log(params)
    this.body = yield service.get_search_data(start, end, keyword)
  })
)

app.use(
  controller.get('/ajax/index', function*() {
    this.set('Cache-Control', 'cache')
    this.body = service.get_index_data()
  })
)

app.use(
  controller.get('/ajax/book', function*() {
    this.set('Cache-Control', 'no-cache')
    let params = qs.parse(this.req._parsedUrl.query)
    let id = params.id || ''
    this.body = service.get_book_data(id)
  })
)

app.use(
  controller.get('/ajax/chapter', function*() {
    this.set('Cache-Control', 'no-cache')
    this.body = service.get_chapter_data()
  })
)

app.use(
  controller.get('/ajax/chapter_data', function*() {
    this.set('Cache-Control', 'no-cache')
    let params = qs.parse(this.req._parsedUrl.query)
    let id = params.id || ''
    this.body = service.get_chapter_content_data(id)
  })
)

app.use(
  controller.get('/api', function*() {
    let params = qs.parse(this.req._parsedUrl.query)
    let uri = params.uri ||
    proxy({
      jar: true,
      host: 'http://html.read.duokan.com',
      map: (path) => {
        return path
      }
    })
  })
)
app.use(
  controller.get(
    '/baidu',
    proxy({
      host: 'http://www.baidu.com'
    })
  )
)

// http-proxy-middleware
// app.use("/api", proxy({
//   target: "https://html.read.duokan.com",
//   changeOrigin: true,
//   pathRewrite: {
//     '^/api': ''
//   }
// }))

app.listen(3000)
console.log('koa service runing at http://127.0.0.1:3000')
